//
//  DHS2_SearchView_Brand.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView_Brand.h"
#import "DHS2_SearchRecommendBrandsItemCell.h"

@interface DHS2_SearchView_Brand()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *brandsArr;

@end

@implementation DHS2_SearchView_Brand

+ (DHS2_SearchView_Brand *)shareDHS2_SearchView_Brand{
    DHS2_SearchView_Brand *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView_Brand class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchRecommendBrandsItemCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchRecommendBrandsItemCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

- (NSMutableArray *)brandsArr{
    if (!_brandsArr) {
        _brandsArr = [NSMutableArray array];
    }
    return _brandsArr;
}

//搜索
- (void)searchData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"品牌" forKey:@"type"];
    [params setObject:self.keyWord forKey:@"key"];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    [[DHNetAPIClient shareDHNetAPIClient] request_app_searchWithParams:params AndBlock:^(id data, NSError *error) {
        if (data) {
            [self.tableView.mj_header endRefreshing];
            
            [self.brandsArr removeAllObjects];
            
            NSDictionary *dic = [data objectForKey:@"results"];
            [self.brandsArr addObjectsFromArray:[dic objectForKey:@"brands"]];
            
            if (self.brandsArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
        }
    }];
}

- (void)searchMoreData{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.brandsArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.brandsArr objectAtIndex:indexPath.row];
    NSArray *arr = [dic objectForKey:@"products"];
    if (arr.count % 2 ==0) {
        return 78 + (arr.count / 2) * 280;
    }else{
        return 78 + (arr.count / 2 + 1) * 280;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHS2_SearchRecommendBrandsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchRecommendBrandsItemCell forIndexPath:indexPath];
    NSDictionary *dic = [self.brandsArr objectAtIndex:indexPath.row];
    [cell updateCell:dic];
    return cell;
}

@end
